package main.java.com.async.tools;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * @author Administrator
 *
 */
public class CustomizeUtil {
	
	private boolean _isRecord = true;
	private String _msgFileName = "msg_record";
	private String _fileExtension = ".txt";
	
	/**
	 * 构造函数，默认文件名为msg_record，扩展名.txt
	 */
	public CustomizeUtil() {
		
	}
	
	/**
	 * 重载构造函数
	 * @param msgFileName 文件名
	 * @param fileExtension 文件扩展名(带.)
	 */
	public CustomizeUtil(String msgFileName, String fileExtension) {
		// TODO Auto-generated constructor stub
		this._msgFileName = msgFileName;
		this._fileExtension = fileExtension;
	}
		
	/**
	 * 设置文件名
	 * @param msgFileName
	 */
	public void setMsgFileName(String msgFileName) {
		this._msgFileName = msgFileName;
	}
	
	/**
	 * 是否需要写出信息
	 * @param isRecord 布尔值,默认true
	 */
	public void isSetRecord(boolean isRecord) {
		this._isRecord = isRecord;
	}
		
	
	/**
	 * 清除对应路径的文件
	 * @param fileFullPath
	 */
	public void clean(String fileFullPath) {
		File tempFile = new File(fileFullPath);
		if(tempFile.exists()) {
			tempFile.delete();
		}
	}
	
	/**
	 * 检测相应的文件是否存在
	 * @param fileFullPath 文件名，包含完整的路径
	 * @return 返回布尔值
	 */
	public Boolean checkFileExist(String fileFullPath) {
		File tempFile = new File(fileFullPath);
		if(!tempFile.exists()) {
			return false;
		}
		return true;
	}
	
	/**
	 * 静态方法
	 * 检测相应的文件是否存在
	 * @param fileFullPath 文件名，包含完整的路径
	 * @return 返回布尔值
	 */
	public static Boolean checkFileExistStatic(String fileFullPath) {
		File tempFile = new File(fileFullPath);
		if(!tempFile.exists()) {
			return false;
		}
		return true;
	}
		
	/**
	 * 字符串写出，如果初始化的时候没有设置文件名和扩展名，将按照默认的名称输出
	 * @param msgContent 字符串内容
	 * @throws IOException
	 */
	public void msgRecordToFile(String msgContent) throws IOException {
		if(_isRecord) {
			PrintWriter pWriter = new PrintWriter(new BufferedWriter(new FileWriter(_msgFileName + _fileExtension)), true);
			pWriter.write(msgContent);
			pWriter.close();
		}
	}
	
	/**
	 * 输出文件到指定路径
	 * @param msgContent 字符串内容
	 * @param fileFullPath 包含文件名及扩展名在内的完整路径
	 * @throws IOException
	 */
	public void msgRecordToFile(String msgContent, String fileFullPath) throws IOException {
		if(_isRecord) {
			PrintWriter pWriter = new PrintWriter(new BufferedWriter(new FileWriter(fileFullPath)), true);
			pWriter.write(msgContent);
			pWriter.close();
		}
	}
	
	/**
	 * 读取文本文件或json文件，并转换成字符串
	 * 
	 * @param fileFullPath 完整的文件路径
	 * @return 返回文本字符串
	 * @throws IOException
	 */
	public static String readFileToString(String fileFullPath) throws IOException {
		File file = new File(fileFullPath);
		if(!file.exists()) {
			System.out.println("需要被读取的文件不存在!");
			return null;
		}
		InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "UTF-8");
		BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
		StringBuffer fileContentBuffer = new StringBuffer();
		String lineContent = bufferedReader.readLine();
		
		while (lineContent != null) {
			fileContentBuffer.append(lineContent);
			lineContent = bufferedReader.readLine();
		}
		
		inputStreamReader.close();
		bufferedReader.close();
		inputStreamReader.close();
		return fileContentBuffer.toString();
	}
}
